﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace WebApplication1.Account
{
    public partial class Register : System.Web.UI.Page
    {
        sv_QuanLyNhaSach.AdapterClient xuly = new sv_QuanLyNhaSach.AdapterClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["captcha"] = null;
                imgCaptcha.ImageUrl= CreateCaptcha();
            }
        }

        private string CreateCaptcha()
        {
            Bitmap bmp = new Bitmap(120, 30);
            Graphics g = Graphics.FromImage(bmp);
            HatchBrush hb = new HatchBrush(HatchStyle.Wave, Color.White, Color.Wheat);
            g.FillRectangle(hb, 0, 0, 120, 30);
            g.FillRegion(hb, g.Clip);
            string text = "0123456789abcdefghjkmmnopqrtuvwxyzABCDEFGHJKMMNOPQRSTUVWXYZ";
            Font f = new Font("arial", 14f);
            Random r = new Random();
            int x = 7, y = 7;
            string s1 = "";
            for (int i = 0; i < 6; i++)
            {
                y = r.Next(1, 10);
                string s = text.Substring(r.Next(0, text.Length), 1);
                s1 = s1 + s;
                g.DrawString(s, f, Brushes.Navy, x, y);
                SizeF size = g.MeasureString(s, f);
                x += (int)size.Width;
            }

            Session["captcha"] = s1;

            string fileName = "~/Image/Captcha.jpg";
            bmp.Save(Server.MapPath(fileName));
            g.Dispose();
            bmp.Dispose();
            return fileName;
        }

        protected void imbreload_Click(object sender, ImageClickEventArgs e)
        {
            Session["captcha"] = null;
            imgCaptcha.ImageUrl = CreateCaptcha();
        }

        protected void CreateUserButton_Click(object sender, EventArgs e)
        {
            if (!txt_Captcha.Text.Equals(Session["captcha"].ToString(), StringComparison.OrdinalIgnoreCase))
            {
                lbl_mess2.Text = "Vui lòng nhập đúng chuỗi xác nhận";
                Session["captcha"] = null;
                imgCaptcha.ImageUrl = CreateCaptcha();
                txt_Captcha.Text = "";
            }
            else if (KiemTraTonTaiTDN() != false)
            {
                lbl_mess.Text = "Tên đăng nhập này đã được sử dụng.";
                Session["captcha"] = null;
                imgCaptcha.ImageUrl = CreateCaptcha();
                txt_Captcha.Text = "";
            }
            else if (KiemTraTonTaiEmail() != false)
            {
                lbl_mess.Text = "Email này đã được sử dụng.";
                Session["captcha"] = null;
                imgCaptcha.ImageUrl = CreateCaptcha();
                txt_Captcha.Text = "";
            }
            else
            {
                string matk = TaoTKMoi();
                if (!xuly.InsertTKOL(matk, txt_UserName.Text, txt_Password.Text, txt_diachi.Text, txt_dienthoai.Text, txt_Email.Text, DateTime.Now))
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Tài khoản không thể tạo.\\n\\n Vui lòng kiểm tra lại thông tin');", true);
                    Session["captcha"] = null;
                    imgCaptcha.ImageUrl = CreateCaptcha();
                    txt_Captcha.Text = "";
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Tài khoản đã được tạo thành công.');location.href='../TrangChu.aspx';", true);
                }
            }
        }

        private string TaoTKMoi()
        {
            DataSet ds_tam = new DataSet();
            string mamoi;
            string maTam = "TKO";
            ds_tam.ReadXml(new StringReader(xuly.selectqueryByText("select top 1 MATKOL from TaikhoanOnline where MaTKOL like '%" + maTam + "%'order by MATKOL desc")));
            if (ds_tam.Tables.Count == 0)
                mamoi = maTam + "0001";
            else
            {
                string matam = ds_tam.Tables[0].Rows[0][0].ToString();
                string kituso = matam.Substring(3, 4).ToString();
                int somoi = int.Parse(kituso) + 1;
                mamoi = maTam + somoi.ToString("0000");
            }
            return mamoi;    
        }

        public bool KiemTraTonTaiTDN()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(xuly.selectqueryByText("select TenDangNhap from taikhoanonline where tendangnhap= '"+ txt_UserName.Text.ToString() +"'")));
            if (ds.Tables.Count > 0)
                return true;
            else
                return false;
        }

        public bool KiemTraTonTaiEmail()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(xuly.selectqueryByText("select Email from taikhoanonline where email= '" + txt_Email.Text.ToString() + "'")));
            if (ds.Tables.Count > 0)
                return true;
            else
                return false;
        }
    }
}